package com.hang.controller;

import com.hang.common.Result;
import com.hang.entity.Message;
import com.hang.model.dto.request.MessageQueryDTO;
import com.hang.model.vo.MessageInfoVOList;
import com.hang.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "消息管理模块")
@RestController
@RequestMapping("message")
@Slf4j
public class MessageController {
    @Resource
    private IMessageService messageService;

    @ApiOperation(value = "用户查看自己的消息列表")
    @PostMapping("list")
    public Result<MessageInfoVOList> getUserMessageInfo(
            @RequestBody MessageQueryDTO messageQueryDTO
    ){
        MessageInfoVOList messageInfoVOList = messageService.getUserMessageInfoList(messageQueryDTO);
        return Result.ok(messageInfoVOList);
    }

    @ApiOperation(value = "读取消息[选择我已阅读标记为已读]")
    @GetMapping("read")
    public Result<Object> readMessage(
            @RequestParam("messageId") @ApiParam(value = "消息id" ,required = true) Long messageId
    ){
        messageService.lambdaUpdate().set(Message::getIsRead,1).eq(Message::getMessageId,messageId).update();
        return Result.ok();
    }

    @ApiOperation(value = "删除消息")
    @DeleteMapping("delete")
    public Result<Object> deleteMessage(
            @RequestParam("messageId") @ApiParam(value = "消息id" ,required = true) Long messageId
    ){
        messageService.lambdaUpdate().eq(Message::getMessageId,messageId).remove();
        return Result.ok().message("删除成功");
    }

    @ApiOperation(value = "清空已读消息")
    @GetMapping("removeBatch")
    public Result<Object> removeMessage(
            @RequestParam("userId") @ApiParam(value = "用户id", required = true) Long userId
    ){
        messageService.lambdaUpdate().eq(Message::getReceiveId,userId).eq(Message::getIsRead,1).remove();
        return Result.ok().message("已清空所有已读消息");
    }
}
